package Readers;

import java.io.IOException;
import java.net.URL;
import java.util.Vector;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import OddsObjects.NumericOdd;
import OddsObjects.OddsTemplate;
import OddsObjects.Soccer.DoubleChance;
import OddsObjects.Soccer.DrawNoBet;
import OddsObjects.Soccer.FirstGoalTime;
import OddsObjects.Soccer.FirstToScore;
import OddsObjects.Soccer.HalfTimeResult;
import OddsObjects.Soccer.HalfWithMostGoals;
import OddsObjects.Soccer.Handicap;
import OddsObjects.Soccer.OverUnder;
import OddsObjects.Soccer.TeamGoals;
import OddsObjects.Soccer.TeamsToScore;
import OddsObjects.Soccer.TotalGoalMinutes;
import OddsObjects.Soccer.TotalGoals;
import OddsObjects.Soccer.WinLoseTie;

public class Triple8SportReader extends SoccerReaderTemplate {

	@Override
	public Vector<OddsTemplate> ExtractAllAvailableOdds(URL pageUrl) {
		Document InDoc6 = null;
		try {
			InDoc6 = Jsoup.connect(pageUrl.toString()).get();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return ( QuincyParser(InDoc6) );
	}

	private enum OddsName {
		NINETYMINUTES("Win/Draw/Win "),
		DRAWNOBET("Draw No Bet "),
		TOTALGOALS("Total Goals "),
		OVERUNDER("Total Goals Over/Under "),
		HANDICAP("Handicap "),
		DOUBLECHANCE("Double Chance "),
		HALFTIMEFULLTIME("Half-Time/Full-Time "),
		HALFTIMERESULT("Half-Time Result "),
		FIRSTTOSCORE("Team to Score First "),
		FIRSTGOALTIME("Time of First Goal "),
		TOTALGOALMINUTES("Total Goal Minutes "),
		HALFWITHMOSTGOALS("Half With Most Goals "),
		TIMEOFFIRSTGOAL("Time of First Goal (Brackets) "),
		TEAMSTOSCORE("Teams to Score "),
		TEAMGOALS("Team Goals ");

		private final String name;

		OddsName( String name ) {
			this.name = name;
		}
		
		public static OddsName find(String nameToFind) {
			for( OddsName on : OddsName.values()){
				if( on.toString().equals(nameToFind) ){
					return on;
				}
			}
			return null;
		}
		
		public String toString() {
			return name;
		}
	}

	private OddsTemplate CreateFormalOdd( String name, Vector<NumericOdd> odds) {
		OddsName on = OddsName.find(name);
		if( on == null )
			return null;
		switch(on) {
		case NINETYMINUTES:
			WinLoseTie wlt = new WinLoseTie(odds);
			return wlt;
		case DRAWNOBET:
			DrawNoBet dnb = new DrawNoBet(odds);
			return dnb;
		case TOTALGOALS:
			return( new TotalGoals(odds));
		case OVERUNDER: 
			OverUnder ou = new OverUnder(odds);
			return(ou);
		case HANDICAP:
			return(new Handicap(odds));
		case DOUBLECHANCE:
			DoubleChance dc = new DoubleChance(odds);
			return dc;
		case HALFTIMEFULLTIME:
			//HalfTimeFullTime htft = new HalfTimeFullTime(odds);
			return(null);
			//TODO: return(htft);
		case HALFTIMERESULT:
			HalfTimeResult htr = new HalfTimeResult(odds);
			return htr;
		case FIRSTTOSCORE:
			FirstToScore fts = new FirstToScore(odds);
			return fts;
		case FIRSTGOALTIME:
			FirstGoalTime fg = new FirstGoalTime(odds);
			return fg;
		case TOTALGOALMINUTES:
			TotalGoalMinutes tgm = new TotalGoalMinutes(odds);
			return tgm;
		case HALFWITHMOSTGOALS:
			HalfWithMostGoals hwmg = new HalfWithMostGoals(odds);
			return hwmg;
		case TIMEOFFIRSTGOAL:
			//TimeOfFirstGoal tofg = new TimeOfFirstGoal(odds);
			//return tofg;
			//TODO
			return null;
		case TEAMSTOSCORE:
			return null;
//			TeamsToScore tts = new TeamsToScore(odds);
//			return tts;
		case TEAMGOALS:
			TeamGoals tg = new TeamGoals(odds);
			return tg;

		}

		return null;


	}

	public Vector<OddsTemplate> QuincyParser(Document doc) {
		Vector<OddsTemplate> parsedOdds = new Vector<OddsTemplate>();

		Document InDoc6 = doc;
		//
		Elements e = InDoc6.select("table > tbody > tr > td > form ");
		//System.out.println(e.get(0));

		//this will get the table with the bets out 
		//Elements Game1 = e.select("table.darkLightDarkText");
		Elements Game1 = e.select("table");

		int count = 0;
		//System.out.println(Game1.get(5));
		String Bet_Type = null;
		while(count < Game1.size()){
			//maybe an if statement based on class
			//if class is bgGlassBar it has the title
			//if class is darkLightDarkText it has odds
			//everything else should be useless

			if(Game1.get(count).attr("class").equals("bgGlassBar")){
				Bet_Type = Game1.get(count).select("tbody > tr > td").text();
				//System.out.println(Bet_Type);
			}
			else if (Game1.get(count).attr("class").equals("darkLightDarkText")){
				//System.out.println(Bet_Type);
				if(Bet_Type.equals("Win/Draw/Win ")
						|| Bet_Type.equals("Draw No Bet ")
						|| Bet_Type.equals("Total Goals ")
						|| Bet_Type.equals("Total Goals Over/Under ")
						|| Bet_Type.equals("Handicap ")
						|| Bet_Type.equals("Double Chance ")
						|| Bet_Type.equals("Half-Time/Full-Time ")
						|| Bet_Type.equals("Half-Time Result ")
						|| Bet_Type.equals("Team to Score First ")
						|| Bet_Type.equals("Time of First Goal ")
						|| Bet_Type.equals("Total Goal Minutes ")
						|| Bet_Type.equals("Half With Most Goals ")
						|| Bet_Type.equals("Time of First Goal (Brackets) ")
						|| Bet_Type.equals("Teams to Score ")
						|| Bet_Type.equals("Team Goals ")
						){
					System.out.println("==============");
					System.out.println(Bet_Type);
					System.out.println("==============");

					//each td in the odds 1 has a name and a odd
					Elements Odds1 = Game1.get(count).select("tbody > tr");
					//System.out.println(Odds1);
					
					Vector<NumericOdd> odds = new Vector<NumericOdd>();
					int i = 0;
					while(i < Odds1.size()){
						String Name = Odds1.get(i).select("td").get(0).text();
						String Odd = Odds1.get(i).select("td").get(1).text();
						odds.add(new NumericOdd(Odd, Name));
						System.out.println("Name: "+ Name + " Odds: "+ Odd);
						i++;
					}
					parsedOdds.add(CreateFormalOdd(Bet_Type, odds));
				}
			}
			count++;
		}
		return parsedOdds;
	}

}
